package wkt.question;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.xnzn.framework.secure.filter.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wkt.RequireStudentLogin;
import wkt.WebUtils;

@RestController
@RequestMapping("/exam-tips")
@Validated
public class ExamTipsController {
    @Autowired
    private ExamTipsMapper examTipsMapper;
    @Autowired
    private ExamTipsConfirmMapper examTipsConfirmMapper;

    @PostMapping("/get")
    public ExamTips get(@RequestBody ExamTips examTips) {
        return examTipsMapper.selectOne(Wrappers.lambdaQuery(ExamTips.class)
                .select(ExamTips::getBasicCode, ExamTips::getType, ExamTips::getContent)
                .eq(ExamTips::getBasicCode, examTips.getBasicCode())
                .eq(ExamTips::getType, examTips.getType()));
    }

    @PostMapping("/get-for-show")
    public ExamTips getForShow(@RequestBody ExamTips examTips) {
        ExamTips examTips1 = get(examTips);
        if (examTips1 == null) examTips1 = new ExamTips(examTips.getBasicCode(), examTips.getType(), "");
        if (CharSequenceUtil.isNotBlank(examTips1.getContent())) {
            examTips1.setContent(examTips1.getContent().replaceFirst("<p>(.*?)</p>", "$1").replaceAll("(<img\\s.*\\s)width=\".*\"\\sheight=\".*\">", "$1 style='max-width:50px;max-height:30px;'>"));
        }
        return examTips1;
    }

    @PostMapping("/delete")
    @RequiresAuthentication
    public void delete(@RequestBody ExamTips examTips) {
        examTipsMapper.delete(Wrappers.lambdaUpdate(ExamTips.class)
                .eq(ExamTips::getBasicCode, examTips.getBasicCode())
                .eq(ExamTips::getType, examTips.getType()));
    }

    @PostMapping("/upsert")
    @Transactional(rollbackFor = Exception.class)
    @RequiresAuthentication
    public void update(@RequestBody ExamTips examTips) {
        final ExamTips examTips1 = get(examTips);
        if (examTips1 == null) {
            examTipsMapper.insert(examTips);
            return;
        }

        examTipsMapper.update(null, Wrappers.lambdaUpdate(ExamTips.class)
                .eq(ExamTips::getBasicCode, examTips.getBasicCode())
                .eq(ExamTips::getType, examTips.getType())
                .set(ExamTips::getContent, examTips.getContent())
        );
    }

    @PostMapping("/confirm")
    @Transactional(rollbackFor = Exception.class)
    @RequireStudentLogin
    public void update(@RequestBody ExamTipsConfirm confirm) {
        confirm.setStudentId(WebUtils.studentId());

        final Long confirmedTipsCount = examTipsConfirmMapper.selectCount(Wrappers.lambdaQuery(ExamTipsConfirm.class)
                .eq(ExamTipsConfirm::getBasicCode, confirm.getBasicCode())
                .eq(ExamTipsConfirm::getStudentId, confirm.getStudentId()));
        if (confirmedTipsCount > 0) return;
        examTipsConfirmMapper.insert(confirm);
    }
}
